import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { showToast } from 'vant';
import { getUserInfo } from "../api/userInfoView"

export const useStore = defineStore('counter', () => {
    let payClassJson: any = window.localStorage.getItem("payClass");
    let router: any = useRouter()
    let token = localStorage.getItem('token');
    const payClass = ref(JSON.parse(payClassJson) || []);
    let CollectClassListJson: any = ref(window.localStorage.getItem("CollectClassList"));
    const CollectClassList :any= ref(JSON.parse(CollectClassListJson.value) || []);
    const userid: any = ref(window.localStorage.getItem("userId"))
    //收藏课程
    const addCollect = (classDes: any, price: any) => {
        console.log("pinia中的方法");
        if (token == undefined) {
            showToast('请登录');

            router.push({
                path: '/register',
            })
        }
        else {
            let index = CollectClassList.value.findIndex((item: any) => {
                return item.data.groupId == classDes.groupId;
            });
            if (index != -1) {
                CollectClassList.value[index] = classDes;
            } else {
                CollectClassList.value.push({
                    price,
                    data: classDes
                });
            }
            console.log("CollectClassList", CollectClassList.value);
            localStorage.setItem("CollectClassList", JSON.stringify(CollectClassList.value));
        }


    }
    const unCollect = (classDes: any) => {
        console.log(classDes);
        let index = CollectClassList.value.findIndex((item: any) => {
            return item.groupId == classDes.groupId;
        });
        CollectClassList.value.splice(index, 1);
        localStorage.setItem("CollectClassList", JSON.stringify(CollectClassList.value));
    }
      // 获取登录以后头像名字用于发布
         // 用户信息,头像,nickname
         const userInfoheard: any = ref([])
         const getUserInfoFun = (): any => {
             getUserInfo(userid.value).then((data: any) => {
                 console.log("getUserInfo55555=>", data)
                 userInfoheard.value = data.data;
                 console.log("pinia23333333=>",userInfoheard.value);
             })
         }
    return { payClass, CollectClassList, addCollect, unCollect,getUserInfoFun,userInfoheard }


})